﻿using System;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Zove.Scripting.Core;

namespace Zove.Desktop.DragAndRun {
    public partial class Form1 : Form {

        private string defaultEditor;

        public Form1() {
            InitializeComponent();

            this.tbOutput.DragEnter += new DragEventHandler(Form1_DragEnter);
            this.tbOutput.DragDrop += new DragEventHandler(Form1_DragDrop);
        }

        public Form1(string fileName)
            : this() {
            this.fileName = fileName;
            this.Load += new EventHandler(Form1_Load);
        }

        public TextBox Output {
            get;
            set;
        }

        public void Form1_Load(object sender, EventArgs e) {
            string fontType = ConfigurationManager.AppSettings["font type"];
            double size = Convert.ToDouble(
                ConfigurationManager.AppSettings["font size"]);
            Font font = new Font(fontType, (float)size);
            this.tbOutput.Font = font;
            defaultEditor = ConfigurationManager.AppSettings["editor"];

            if (!string.IsNullOrEmpty(this.fileName)) {
                Run(this.fileName);
            }
        }

        public void Form1_DragEnter(object sender, DragEventArgs e) {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true) {
                // allow them to continue
                // (without this, the cursor stays a "NO" symbol
                e.Effect = DragDropEffects.All;
            }
        }

        public void Form1_DragDrop(object sender, DragEventArgs e) {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            this.fileName = files[0];

            this.Run(this.fileName);
        }

        private string fileName = string.Empty;

        private void btnRerun_Click(object sender, EventArgs e) {
            Run(this.fileName);
        }

        public void Run(string fileName) {
            if (string.IsNullOrEmpty(fileName)) {
                return;
            }

            TextBoxWriter writer = new TextBoxWriter(this.tbOutput);
            Console.SetOut(writer);

            this.Text = "ZoveScript - [" + fileName + "]";
            string source = System.IO.File.ReadAllText(fileName);
            ZoveScriptingObject script = new ZoveScriptingObject(source);

            //try {
                script.Run();
            //} catch (Exception ex) {
            //    Console.WriteLine(ex.Message);
            //}
        }

        private void btnStop_Click(object sender, EventArgs e) {

        }

        private void btnClear_Click(object sender, EventArgs e) {
            this.tbOutput.Text = string.Empty;
        }

        private void cmdEdit_Click(object sender, EventArgs e) {
            Process process = new Process();

            string editor = ConfigurationManager.AppSettings["editor"];
            process.StartInfo.FileName = string.IsNullOrEmpty(editor) ? "notepad.exe" : editor;
            process.StartInfo.Arguments = this.fileName;
            
            process.Start();
        }

        private void btnAbout_Click(object sender, EventArgs e) {
            MessageBox.Show(this.fileName, "About this script ...");
        }

        private void cmdExport_Click(object sender, EventArgs e) {
            SaveFileDialog d = new SaveFileDialog();

            if (d.ShowDialog() == DialogResult.OK) {
                File.WriteAllText(d.FileName, this.tbOutput.Text);
            }
        }

        private void cmdConfgure_Click(object sender, EventArgs e) {
            ConfigurationView v = new ConfigurationView();

            if (v.ShowDialog() == DialogResult.OK) {
                this.tbOutput.Font = v.SelectedFont;
            }
        }
    }
}
